/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : GetCompanyMatchesByName
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Country
 * Argument4 = Name to Match
 */
class CompanyMatchByName{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid         = null;
	      string partnerPassword	   = null;
	      string matchCountry		   = null;	
	      string matchName         	   = null;
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid      = args[0].Trim();
	      partnerPassword    = args[1].Trim();
	      matchCountry 	     = args[2].Trim();
	      matchName          = args[3].Trim();
	      
	      GraydonCompanyData             graydonCompanyData           = new GraydonCompanyData();
	      GetCompanyMatches_ResultType    getCompanyMatches_Result    = null;
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                    = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                    = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                  = partnerPassword;
	      
	      // create the matching parameters
	      CompanyMatches_ParametersType companyMatches_Parameters                                    = new CompanyMatches_ParametersType();
	      companyMatches_Parameters.Country                                                          = matchCountry;
	      
	      // create the parameters
	      GetCompanyMatchesByName_ParametersType getCompanyMatchesByName_Parameters                  = new GetCompanyMatchesByName_ParametersType();
	      getCompanyMatchesByName_Parameters.Authentication_Parameters                               = authentication_Parameters;
	      getCompanyMatchesByName_Parameters.CompanyMatches_Parameters                               = companyMatches_Parameters;
	      getCompanyMatchesByName_Parameters.Name                                                    = matchName;
	      
	      // Begin the Async call to "getCompanyMatchesByIdentifier"
	      IAsyncResult ar = graydonCompanyData.BegingetCompanyMatchesByName(getCompanyMatchesByName_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      try
          {
	         // get the results of the match
	         getCompanyMatches_Result = graydonCompanyData.EndgetCompanyMatchesByName(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }      
          
         // get the service log details
         serviceLog(getCompanyMatches_Result.Service_Log);
	      
	      // check if matches were returned
	      if (getCompanyMatches_Result.CompanyMatches != null)
	      {
	         System.Console.WriteLine(" ");
	         CompanyType[] company = getCompanyMatches_Result.CompanyMatches;
	         
	         // get all the companies returned
	         for (int x = 0; x < company.Length; x++)
	         {
	            Console.WriteLine("Company : " + x);
	            Console.WriteLine("Company Name\t\t: " + company[x].Name);
	            
				// get the returned company identifiers for the company
				CompanyIdentifierType[] companyIdentifiers = company[x].CompanyIdentifiers; 
				for (int y = 0; y < companyIdentifiers.Length; y++)
				{
					Console.WriteLine("Identifiers :");																	
					Console.WriteLine("  Type\t\t\t: " + companyIdentifiers[y].Type);									    // output identifier type
					Console.WriteLine("  Identifier\t\t: " + companyIdentifiers[y].Identifier);						        // output identifier
				}
				
				if (company[x].RegisteredAddress != null)
				{
					Console.WriteLine("Registered Address :");
					if (company[x].RegisteredAddress.Building != null)
						Console.WriteLine("  Building\t\t: " + company[x].RegisteredAddress.Building);			            // output the building
					if (company[x].RegisteredAddress.Street != null)
						Console.WriteLine("  Street\t\t: " + company[x].RegisteredAddress.Street);				            // output the street
					if (company[x].RegisteredAddress.Town != null)
						Console.WriteLine("  Town\t\t: " + company[x].RegisteredAddress.Town);					            // output the town
					if (company[x].RegisteredAddress.City != null)
						Console.WriteLine("  City\t\t\t: " + company[x].RegisteredAddress.City);				            // output the city
					if (company[x].RegisteredAddress.County != null)
						Console.WriteLine("  County\t\t: " + company[x].RegisteredAddress.County);				            // output the county
					if (company[x].RegisteredAddress.PostCode != null)
						Console.WriteLine("  PostCode\t\t: " + company[x].RegisteredAddress.PostCode);			            // output the postCode
				}
				
				if (company[x].TradingAddress != null)
				{
					Console.WriteLine("Trading Address :");
					if (company[x].TradingAddress.Building != null)
						Console.WriteLine("  Building\t\t: " + company[x].TradingAddress.Building);				            // output the building
					if (company[x].TradingAddress.Street != null)
						Console.WriteLine("  Street\t\t: " + company[x].TradingAddress.Street);					            // output the street
					if (company[x].TradingAddress.Town != null)
						Console.WriteLine("  Town\t\t: " + company[x].TradingAddress.Town);						            // output the town
					if (company[x].TradingAddress.City != null)
						Console.WriteLine("  City\t\t\t: " + company[x].TradingAddress.City);		      			        // output the city
					if (company[x].TradingAddress.County != null)
						Console.WriteLine("  County\t\t: " + company[x].TradingAddress.County);					            // output the county
					if (company[x].TradingAddress.PostCode != null)
						Console.WriteLine("  PostCode\t\t: " + company[x].TradingAddress.PostCode);				            // output the postCode
				}
				
				if (company[x].DateOfIncorporation != null)
					Console.WriteLine("Incorporation Date\t: " + "{0:d}",company[x].DateOfIncorporation);	                // output date of incorporation
				if (company[x].DateAccountsFiled != null)
					Console.WriteLine("Accounts Filed Date\t: " + "{0:d}",company[x].DateAccountsFiled);	                // output date of accounts filed
				if (company[x].DateOfAnnualReturn != null)
					Console.WriteLine("Annual Return Date\t: " + "{0:d}",company[x].DateOfAnnualReturn);	                // output date of annual return
				if (company[x].DateOfLatestAccounts != null)
					Console.WriteLine("Latest Accounts Date\t: " + "{0:d}",company[x].DateOfLatestAccounts);                // output date of latest accounts
				if (company[x].Telephone != null)
					Console.WriteLine("Telephone\t\t: " + company[x].Telephone);										    // output the telephone number
				if (company[x].Email != null)
					Console.WriteLine("Email\t\t\t: " + company[x].Email);											        // output the email address
				if (company[x].Facsimile != null)
					Console.WriteLine("Fax\t\t\t: " + company[x].Facsimile);											    // output the fax address
				if (company[x].CompanyMatchIdentifier != null)
					Console.WriteLine("Company Match Identifier: " + company[x].CompanyMatchIdentifier);			        // output the company match identifier
					
				// get the returned company identifiers for the company
				if (company[x].Activites != null)
				{
					ActivityType[] activities = company[x].Activites; 
					for (int y = 0; y < activities.Length; y++)
					{
						Console.WriteLine("Activities :");																	
						Console.WriteLine("  Type\t\t\t: " + activities[y].Type);											// output activity type
						Console.WriteLine("  Description\t\t: " + activities[y].Description);								// output acitivity description
					}
				}
				
				Console.WriteLine(" ");	
	         }
	         
	      }
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}   
   
}